
define(['zepto', 'butterfly/view','../../bsl',
    'text!./accelerometer_shake.html'
], function($, View, bsl, accelerometer_shake) {
    var me = null;

    var view = View.extend({
        p:null,
        iLast:2,MAX:20,
        up:null,down:null,offset:50,
        t:null,
        events: {
            "tap #doc_back":"Doc_back",
            "tap #back" : "Back",
        },
            
        render: function() {
            me = this;
        },

        onShow: function() {
            this.bslReady();
            this.domcotentload();
            this.unlockback();
      
        },
        
        // H5 bsl事件处理
        bslReady:function (){
            // 锁定只能竖屏显示
            bsl.screen.lockOrientation( "portrait-primary" );
            // 监听加速度
            bsl.accelerometer.watchAcceleration( function ( a ) {
                if(!up){
                    return;
                }
                if ( !p && ( Math.abs(a.xAxis)+Math.abs(a.yAxis)+Math.abs(a.zAxis) > MAX ) ){
                    // Play audio
                    p = bsl.audio.createPlayer("_www/audio/shake.wav");
                    p.play();
                    setTimeout( function(){
                        // Change background image
                        var index = Math.round(Math.random()*3+1);
                        if ( iLast == index ) {
                            index++;
                            if ( index > 4 ) {
                                index = 1;
                            }
                        }
                        document.body.style.backgroundImage="url(images/shake/"+index+".jpg)";
                        iLast = index;
                        // Stop play audio
                        p.stop();
                        delete p;
                        p = null;
                    }, 2000 );
                    // Animation
                    offset=up.offsetHeight/2;
                    up.style.webkitTransform = "translateY(-"+offset+"px)";
                    up.style.msTransform = "translateY(-"+offset+"px)";
                    down.style.webkitTransform = "translateY("+offset+"px)";
                    down.style.msTransform = "translateY("+offset+"px)";
                    if ( t ) {
                        clearTimeout( t );
                    }
                    t = setTimeout( function() {
                        t = null;
                        up.style.webkitTransform = "";
                        up.style.msTransform = "";
                        down.style.webkitTransform = "";
                        down.style.msTransform = "";
                    }, 700 );
                }
            }, function ( e ) {
                //outSet( "Watch failed: "+e.message );
            }, {frequency:100} );
        },

        // 监听DOMContentLoaded事件
        domcotentload: function(){
            document.addEventListener("DOMContentLoaded",function(){
                up=document.getElementById("up");
                down=document.getElementById("down");
                offset=up.offsetHeight/2;
            },false);
        },
        // 解锁并关闭
        unlockback:function (){
            bsl.screen.unlockOrientation();
        },

       
        Back : function(e){
            this.unlockback();
            window.history.go(-1);
        },
        

    });
    return view;
});
